package com.teacher.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.common.model.BaseUser;
import com.common.model.Profession;
import com.common.model.Project;
import com.common.model.StudentUser;
import com.common.model.TeacherUser;
import com.common.model.TimePlan;
import com.common.util.Md5Encrypt;
import com.student.model.IntentProject;
import com.teacher.dao.TeacherDao;
import com.teacher.model.ProjAndProf;
import com.teacher.model.TeachCourse;

/**
 * 教师版块业务逻辑处理类
 * 
 * @author XHFSJ
 * 
 */
public class TeacherService {

	private Logger log = org.slf4j.LoggerFactory.getLogger(this.getClass());
	/**
	 * 内置tdao对象 采用自动注入技术
	 */
	@Autowired
	private TeacherDao tdao;

	/**
	 * 用于对教师重置密码后的业务逻辑处理
	 * 
	 * @param password
	 *            新密码
	 * @param no
	 *            教师编号
	 * @return 返回true/false,表示操作的成功与否
	 */
	public boolean UpdateTeacherPassword(String password, String no) {
		int num = tdao.UpdateTeacherPassword(Md5Encrypt.encrypt(password), no);
		if (num == 0)
			return false;
		return true;

	}

	public boolean updateTeacherInfo(TeacherUser teacher) {
		return tdao.updateTeacherInfo(teacher) > 0 ? true : false;
	}

	/**
	 * 发布新题目
	 * 
	 * @param p
	 * @return
	 */
	public boolean AddProject(Project p) {
		int num = tdao.AddProject(p);
//		System.out.println(p.getId());
		if (num > 0)
			return true;
		return false;
	}

	/**
	 * 根据教师的职工号获取教师基本信息
	 * 
	 * @param no
	 * @return
	 */
	public TeacherUser GetTeacherInfo(String no) {
		if ("".equals(no) || no == null) {
			log.info("教师编号不符合要求");
			throw new IllegalArgumentException("教师编号不符合要求");
		}

		TeacherUser teacher = tdao.getTeacherInfo(no);
		return teacher;
	}
	
	public List<TeacherUser> getTeacherListByTeacherId(Integer id,Integer departId){
		return tdao.getTeacherListByTeacherId(id, departId);
	}

	/**
	 * 根据专业名称获取专业的编号
	 * 
	 * @param name
	 * @return
	 */
	public Integer getProfessionIDByName(String name) {
		System.out.println(name);
		Integer id = tdao.getIdByProfessionName(name);
		return id;
	}


	/**
	 * 查询所有专业的名称和编号
	 * 
	 * @return
	 */
	public List<Profession> getAllProfession(Integer tea_id) {
		return tdao.getAllProfession(tea_id);
	}

	public void AddProjAndProf(ProjAndProf pap) {
//		System.out.println("insert projandprof");
		tdao.AddProjAndProf(pap);
	}

	public Project getProjectByName(String name) {
		return tdao.getProjectByName(name);
	}

	public List<Project> getProjectsOfTeacher(int tea_id) {
		return tdao.getProjectsOfTeacher(tea_id);
	}

	public boolean DelProject(Integer id) {
		try {
//			System.out.println(id);
			int num = tdao.DeleteProjectById(id);
//			System.out.println("nums:" + num);
			if (num != 0)
				return true;
			else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public Project GetProjectById(Integer id) {
		return tdao.GetProjectById(id);
	}

	public boolean UpdateProject(Project project) {
		int num = tdao.UpdateProject(project);
		return num > 0 ? true : false;
	}

	public List<Project> GetAllProjects() {
		return tdao.exportProject();
	}

	public List<Project> getProjectsByStatus(String status, Integer tea_id) {
		return tdao.GetProjectsByStatus(status, tea_id);
	}

	public Project GetProjectInfoById(Integer id) {
		return tdao.getProjectAllInfoById(id);
	}

	/**
	 * 查询题目意向信息
	 * @param proj_id
	 * @return
	 */
	public List<IntentProject> GetIntentProjectByProjId(Integer proj_id,Long maxLevel){
		return tdao.GetIntentProjectsByProjId(proj_id,maxLevel);
	}
	
	public boolean SelectStudentOfProject(Integer stu_id,Integer proj_id){
//		System.out.println("stu_id="+stu_id+",proj_id="+proj_id);
		int num = tdao.selectStudentOfProject(stu_id, proj_id);
		return num>0;
	}
	
	/**
	 * 取消已选择的学生
	 * @param proj_id
	 * @return
	 */
	public boolean cancelSel(Integer proj_id){
		return tdao.cancelSel(proj_id)>0;
	}
	
	/**
	 * 获取教师教学计划信息
	 * @param tea_id
	 * @return
	 */
	public List<TeachCourse> GetTeachingProgressByTeacherId(Integer tea_id){
		return tdao.GetTeachingCourseByTeacherId(tea_id);
	}
	
	public List<Project> getAuthProject(Integer tea_id){
		return tdao.getAuthProject(tea_id);
	}
	
	public TimePlan getTimePlanByTeacherId(Integer tea_id){
		return tdao.getTimePlanByTeacherId(tea_id);
	}
	
	/**
	 * 根据类型查询教师或者学生信息
	 * @param type
	 * @param no
	 * @return
	 */
	public BaseUser getUserInfoByNo(String type,String no){
		switch (type) {
		case "student":
			return tdao.getStudentInfoByNo(no);
		case "teacher":
			return tdao.getTeacherInfoByNo(no);
		default:
			return null;
		}
	}
	public StudentUser getStudentInfoByNo(String no){
		return tdao.getStudentInfoByNo(no);
	}
	
	public TeacherUser getTeacherInfoByNo(String no){
		return tdao.getTeacherInfo(no);
	}
	
	
	
	/**
	 * 上传题目任务书
	 * @param taskBook
	 * @param id
	 * @return
	 */
	public boolean uploadTaskBook(String taskBook,Integer id){
		int count = tdao.uploadTaskBook(taskBook, id);
		return count > 0;
	}
	
	

	public boolean AuthProject(Integer status,String reason,Integer id){
		int count = tdao.AuthProj(status, reason, id);
		return count > 0;
	}
	
	public boolean setReasonToNotChoose(String reason,Integer id){
		return tdao.setReasonToNotChoose(reason, id)>0;
	}
	
	public List<Project> GetProjectsByKeywordOrStatus(String word,String status,Integer tea_id){
		word = "%"+word+"%";
		return tdao.GetProjectsByKeywordOrStatus(status, word,tea_id);
	}
	
	/**
	 * 根据教师名称以及部门编号查询教师信息
	 * @param name
	 * @param departId
	 * @return
	 */
	public TeacherUser getTeacherInfoByName(String name,Integer departId){
		return tdao.getTeacherInfoByName(name, departId);
	}
	
	public String getPlanByCourseId(Integer courseId){
		return tdao.getPlanByCourseId(courseId);
	}
	
	/**
	 * 上传教学计划书
	 * @param planBookName
	 * 		计划书名称，包含URL全路径名
	 * @param courseId
	 * 		课程ID
	 * @return
	 */
	public boolean uploadCourseTeachingPlanBook(String planBookName,Integer courseId){
		int num = 0;
		num = tdao.updateTeachingPlanBookByCourseId(planBookName, courseId);
		return num > 0;
	}
	
	public List<Project> getPassedProjectOfTeacher(Integer id){
		return tdao.getPassedProjectOfTeacher(id);
	}
	
	public int SelectStuByTeacher(Integer stuId,Integer projId){
		return tdao.SelectStuByTeacher(stuId, projId);
	}
	
	public boolean setScore(Integer id,Integer syScore,Integer pyScore,Integer dbScore,String dbmember,Date date){
		int num = 0;
		num = tdao.setStudentScore(id, syScore, pyScore, dbScore,dbmember,date);
		return num > 0;
	}
	
	public boolean existsScore(Integer id){
		int num = 0;
		num = tdao.existsScore(id);
		return num > 0;
	}
	
	public boolean updateScore(Integer projId,Integer syScore,Integer pyScore,Integer dbScore){
		return tdao.updateScore(projId, syScore, pyScore, dbScore,new Date())>0;
	}
	
	public List<Project> generateResult(){
		return tdao.generateResult();
	}
}
